package com.dy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.mapper.DyMessageMapper;
import com.dy.pojo.dto.DyMessageDTO;
import com.dy.pojo.entity.DyMessageEntity;
import com.dy.pojo.vo.DyMessageVO;
import com.dy.service.DyMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/6/9 16:49
 * @Version 1.0 (版本)
 */
@Service
public class DyMessageServiceImpl extends ServiceImpl<DyMessageMapper, DyMessageEntity> implements DyMessageService {
    @Autowired
    private DyMessageMapper dymessageMapper;

    @Override
    public List<DyMessageVO> selectAll() {
        List<DyMessageEntity> selectList = dymessageMapper.selectList(new QueryWrapper<DyMessageEntity>().isNull("parent_code"));
        ArrayList<DyMessageVO> listVO = new ArrayList<>();
        for (int i = 0; i < selectList.size(); i++) {
            DyMessageVO dyMessageVO = new DyMessageVO();
            String code= selectList.get(i).getCode();
            List<DyMessageEntity> seedlist = dymessageMapper.selectList(new QueryWrapper<DyMessageEntity>().eq("parent_code", code));
            ArrayList<DyMessageVO> list = new ArrayList<>();
            for (int j = 0; j < seedlist.size(); j++) {
                DyMessageVO messageVO = new DyMessageVO();
                messageVO.setName(seedlist.get(j).getName())
                        .setAvatar(seedlist.get(j).getAvatar())
                        .setEmail(seedlist.get(j).getEmail())
                        .setContent(seedlist.get(j).getContent());
                list.add(messageVO);
            }
            dyMessageVO.setName(selectList.get(i).getName())
                    .setAvatar(selectList.get(i).getAvatar())
                    .setContent(selectList.get(i).getContent())
                    .setEmail(selectList.get(i).getEmail())
                    .setSeed(list);
            listVO.add(dyMessageVO);
        }
        return listVO;
    }

    @Override
    public void create(DyMessageDTO dyMessageDTO,String userCode) {
        DyMessageEntity entity = new DyMessageEntity();
        String code="M"+System.currentTimeMillis();
        entity.setName(dyMessageDTO.getName())
                .setUserCode(userCode)
                .setCode(code)
                .setContent(dyMessageDTO.getContent())
                .setAvatar(dyMessageDTO.getAvatar())
                .setEmail(dyMessageDTO.getEmail());
        dymessageMapper.insert(entity);
    }

    @Override
    public void delete(Long id) {
        dymessageMapper.deleteById(id);
    }

    @Override
    public Boolean replyMessage(DyMessageDTO dyMessageDTO,String userCode) {
        List<DyMessageEntity> selectList = dymessageMapper.selectList(new QueryWrapper<DyMessageEntity>().eq("code", dyMessageDTO.getParentCode()));
        if (selectList.size() == 0) {
            return false;
        }
        DyMessageEntity dyMessageEntity = new DyMessageEntity();
        String code="M"+System.currentTimeMillis();
        dyMessageEntity.setUserCode(userCode)
                .setCode(code)
                .setParentCode(dyMessageDTO.getParentCode())
                .setAvatar(dyMessageDTO.getAvatar())
                .setEmail(dyMessageDTO.getEmail())
                .setContent(dyMessageDTO.getContent())
                .setName(dyMessageDTO.getName());
        dymessageMapper.insert(dyMessageEntity);
        return true;
    }
}
